package com.viettel.pon.Tabs;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.viettel.pon.R;
import com.viettel.pon.common.DataBaseHelper;
import com.viettel.pon.common.GlobalClass;
import com.viettel.pon.form.InfomationForm;

public class ReportActivity extends Fragment {
	private int year;
	private int month;
	private int day;
	private View rootView;
	private Context _context;
	DataBaseHelper db;
	private GlobalClass _glb;
	private ImageButton ibtPicDateSearch;
	private EditText edtDateSearch;
	//private ImageButton ibtLookupData;
	private TextView txtDDmmyyyy;
	private TextView txtPoleNumber;
	private TextView txtHouseNumber;
	private TextView txtPlaceNumber;
	private LinearLayout llOk;
	private TextView txtPointNumber;
	
	private TextView txtPoleNumberNotInsert;
	private TextView txtHouseNumberNotInsert;
	private TextView txtPlaceNumberNotInsert;
	private TextView txtPointNumberNotInsert;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			final Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.activity_report, container, false);
		_context = getActivity().getApplicationContext();
		
		db = new DataBaseHelper(_context);
		_glb = (GlobalClass) _context.getApplicationContext();
		ibtPicDateSearch = (ImageButton) rootView.findViewById(R.id.ibtPicDateSearch);
		edtDateSearch = (EditText) rootView.findViewById(R.id.edtDateSearch);
		txtPointNumber = (TextView) rootView.findViewById(R.id.txtPointNumber);
		txtDDmmyyyy = (TextView) rootView.findViewById(R.id.txtDDmmyyyy);
		txtPoleNumber = (TextView) rootView.findViewById(R.id.txtPoleNumber);
		txtHouseNumber = (TextView) rootView.findViewById(R.id.txtHouseNumber);
		txtPlaceNumber = (TextView) rootView.findViewById(R.id.txtPlaceNumber);
		
		txtPointNumberNotInsert=(TextView) rootView.findViewById(R.id.txtPointNumberNotInsert);
		txtPoleNumberNotInsert= (TextView) rootView.findViewById(R.id.txtPoleNumberNotInsert);
		txtHouseNumberNotInsert=(TextView) rootView.findViewById(R.id.txtHouseNumberNotinsert);
		txtPlaceNumberNotInsert=(TextView) rootView.findViewById(R.id.txtPlaceNumberNotinsert);
 		llOk = (LinearLayout) rootView.findViewById(R.id.llOk);
		
		llOk.setVisibility(View.GONE);
		
		setCurrentDateOnView();
		
		edtDateSearch.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				llOk.setVisibility(View.GONE);
				
			}
		});
		
		ibtPicDateSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				showSelectionDatePopup();
			}
		});
		
		return rootView;
	}
	
	private void showSelectionDatePopup() {

	Calendar cal = Calendar.getInstance();
	int day = cal.get(Calendar.DAY_OF_MONTH);
	int month = cal.get(Calendar.MONTH);
	int year = cal.get(Calendar.YEAR);

	DatePickerDialog dialog = new DatePickerDialog(getActivity(),
			datePickerListener, year, month, day);
	dialog.show();
	}

	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
	
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date date;
			try {
				
				edtDateSearch.setText(selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear);
				txtDDmmyyyy.setText(edtDateSearch.getText().toString().trim() + ":");
				date = sdf.parse(String.valueOf(selectedYear) + "-"
						+ String.valueOf(selectedMonth + 1) + "-"
						+ String.valueOf(selectedDay));
				List<InfomationForm> lstPoint = db.getPositionByDate(date);
				List<InfomationForm> lstPoles = db.getPoleByDate(date);
				List<InfomationForm> lstHouse = db.getHouseByDate(date);
				List<InfomationForm> lstPlace = db.getPlaceByDate(date);
				//anhpt67 edit
				List<InfomationForm> lstPointNotInserted= db.getAllPositionNotInserted();
				List<InfomationForm> lstPolesNotInserted= db.getPoleNotInserted();
				List<InfomationForm> lstHouseNotInserted= db.getHouseNotInserted();
				List<InfomationForm> lstPlaceNotInserted= db.getPlaceNotInserted();
				
				llOk.setVisibility(view.VISIBLE);
				//anhpt67 edit
				Log.e("Size", "Point Size: " + lstPoint.size());
				if(lstPoint.size() > 0){
					txtPointNumber.setText(String.valueOf(lstPoint.size()));
				} else {
					txtPointNumber.setText("");
				}
				
				if(lstPointNotInserted.size() > 0){
					txtPointNumberNotInsert.setText(String.valueOf(lstPointNotInserted.size()));
				}
				else{
					txtPointNumberNotInsert.setText("");
				}
				
				Log.e("Size", "Pole Size: " + lstPoles.size());
				if(lstPoles.size() > 0){
					txtPoleNumber.setText(String.valueOf(lstPoles.size()));
				} else {
					txtPoleNumber.setText("");
				}
				
				if(lstPolesNotInserted.size()>0){
					txtPoleNumberNotInsert.setText(String.valueOf(lstPolesNotInserted.size()));
				}else {
					txtPoleNumberNotInsert.setText("");
				}
				
				Log.e("Size", "House Size: " + lstHouse.size());
				if(lstHouse.size() > 0){
					txtHouseNumber.setText(String.valueOf(lstHouse.size()));
				} else {
					txtHouseNumber.setText("");
				}
				
				if(lstHouseNotInserted.size() > 0){
					txtHouseNumberNotInsert.setText(String.valueOf(lstHouseNotInserted.size()));
				}else {
					txtHouseNumberNotInsert.setText("");
				}
				
				Log.e("Size", "Palce Size: " + lstPlace.size());
				if(lstPlace.size() > 0){
					txtPlaceNumber.setText(String.valueOf(lstPlace.size()));
				} else {
					txtPlaceNumber.setText("");
				}
				
				if(lstPlaceNotInserted.size() > 0){
					txtPlaceNumberNotInsert.setText(String.valueOf(lstPlaceNotInserted.size()));
				}else {
					txtPlaceNumberNotInsert.setText("");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	
	public void setCurrentDateOnView() {

		final Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);

		// set current date into textview
		edtDateSearch.setText(new StringBuilder()
				// Month is 0 based, just add 1
				.append(day).append("/").append(month + 1).append("/")
				.append(year).append(" "));

	}
}
